package com.itlong.cloud.controller.operate;

import com.itlong.cloud.POJO.DTO.operate.OperatePushInfoPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateSavePushInfoDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperatePushInfoOneVO;
import com.itlong.cloud.POJO.VO.operate.OperatePushInfoVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.PushInfoTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperatePushInfoServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营—运营管理模块—消息推送。
 * <desc/>
 *
 * @createDate 2017/10/19.
 */
@RestController
@RequestMapping("/marketer/pushInfo")
public class OperatePushInfoController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(OperatePushInfoController.class);

    //消息推送
    @Autowired
    IOperatePushInfoServiceClient iOperatePushInfoServiceClient;

    /**
     * <desc>
     *      保存消息推送内容。
     * <desc/>
     *
     * @param  pushInfoDTO  添加消息对象
     * @return
     * @author shengen.T
     * @createDate 2017/9/29
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.SAVE_PUSHINFO_LOG_DESC)
    @RequestMapping(path="/savePushInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object savePushInfo(OperateSavePushInfoDTO pushInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateSavePushInfoDTO.class,request);
            //数据添加对象不能为空
            if(null == pushInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断项目编号
            if(StringUtils.isBlank(pushInfoDTO.getProjectIds())){
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            //消息类型
            if(null == pushInfoDTO.getInfoType()){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_TYPE_EMPTY_ERR.getErrorCode());
            }
            pushInfoDTO.setInfoType(PushInfoTypeEnum.OPERATE_PUSH_INFO_TYPE.getType());
            //判断推送时间
            if(StringUtils.isBlank(pushInfoDTO.getBeginDate())&&
                    StringUtils.isBlank(pushInfoDTO.getEndDate())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_TIME_EMPTY_ERR.getErrorCode());
            }
            //判断推送内容
            if(StringUtils.isBlank(pushInfoDTO.getInfoContent())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_CONTENT_EMPTY_ERR.getErrorCode());
            }
            //发布平台 1代表App和代表小程序 3代表家半径物业宝APP
            //发布平台 最新更开 1代表App 2代表小程序 3代表家半径物业宝APP
            if ((null == pushInfoDTO.getPublishPlatform()) || (pushInfoDTO.getPublishPlatform() != 1 && pushInfoDTO.getPublishPlatform() != 2 && pushInfoDTO.getPublishPlatform() != 3)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (pushInfoDTO.getPublishPlatform() == 1 && StringUtils.isBlank(pushInfoDTO.getVersionInfo())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(pushInfoDTO.getTitle())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Integer result = iOperatePushInfoServiceClient.savePushInfo(pushInfoDTO);
            if (result == -1) {
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode(), "请等待工作人员执行完发布过程再操作");
            }
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送save】添加消息推送资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      修改推送消息内容。
     * <desc/>
     *
     * @param  pushInfoDTO 消息推送传输数据对象
     * @return
     * @author shengen.T
     * @createDate 2017/9/29
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_PUSHINFO_LOG_DESC)
    @RequestMapping(path="/updatePushInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object updatePushInfo(OperateSavePushInfoDTO pushInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateSavePushInfoDTO.class,request);
            //数据更新对象不能为空
            if(null == pushInfoDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断消息ID
            if(StringUtils.isBlank(pushInfoDTO.getPushInfoId())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }
            //判断项目编号
            if(StringUtils.isBlank(pushInfoDTO.getProjectIds())){
                return new MessageVO(OperateErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            //消息类型
            if(null == pushInfoDTO.getInfoType()){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_TYPE_EMPTY_ERR.getErrorCode());
            }
            pushInfoDTO.setInfoType(PushInfoTypeEnum.OPERATE_PUSH_INFO_TYPE.getType());
            //判断推送时间
            if(StringUtils.isBlank(pushInfoDTO.getBeginDate())&&
                    StringUtils.isBlank(pushInfoDTO.getEndDate())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_TIME_EMPTY_ERR.getErrorCode());
            }
            //判断推送内容
            if(StringUtils.isBlank(pushInfoDTO.getInfoContent())){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_CONTENT_EMPTY_ERR.getErrorCode());
            }
            //发布平台 1代表App和代表小程序 3代表家半径物业宝APP
            //发布平台 最新更开 1代表App 2代表小程序 3代表家半径物业宝APP
            if ((null == pushInfoDTO.getPublishPlatform()) || (pushInfoDTO.getPublishPlatform() != 1 && pushInfoDTO.getPublishPlatform() != 2 && pushInfoDTO.getPublishPlatform() != 3)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (pushInfoDTO.getPublishPlatform() == 1 && StringUtils.isBlank(pushInfoDTO.getVersionInfo())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(pushInfoDTO.getTitle())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Integer result = iOperatePushInfoServiceClient.updatePushInfo(pushInfoDTO);
            if (result == -1) {
                return new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode(), "请等待工作人员执行完发布过程再操作");
            }
            messageVO=new MessageVO(pushInfoDTO.getPushInfoId(), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送update】添加资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      批量删除推送消息内容。
     * <desc/>
     *
     * @param  pushInfoIds 消息ID
     * @return
     * @author shengen.T
     * @createDate 2017/10/12
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.DEL_PUSHINFO_LOG_DESC)
    @RequestMapping(path="/deletePushInfo",method = RequestMethod.POST)
    @ResponseBody
    public Object deletePushInfo(String pushInfoIds) {
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoIds=request.getAttribute("pushInfoIds")==null?null:request.getAttribute("pushInfoIds").toString();
            if(StringUtils.isBlank(pushInfoIds)){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }
            iOperatePushInfoServiceClient.deletePushInfo(pushInfoIds);
            messageVO=new MessageVO(pushInfoIds, BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送delete】添加资源失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      根据消息ID获取详细信息。
     * <desc/>
     *
     * @param  pushInfoId 消息
     * @return
     * @author shengen.T
     * @createDate 2017/9/29
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "/getPushInfo", method = RequestMethod.POST)
    public Object getPushInfo(String pushInfoId) {
        MessageVO messageVO;
        try {
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pushInfoId=request.getAttribute("pushInfoId")==null?null:request.getAttribute("pushInfoId").toString();
            if(StringUtils.isBlank(pushInfoId)){
                return new MessageVO(OperateErrorCodeEnum.PUSHINFO_ID_EMPTY_ERR.getErrorCode());
            }
            OperatePushInfoOneVO vo =iOperatePushInfoServiceClient.getPushInfo(pushInfoId);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),vo);
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送getOne】获取失败",e);
        }
        return messageVO;

    }

    /**
     * <desc>
     *      获取消息推送分页信息。
     * <desc/>
     *
     * @param  pageDTO 请求参数
     * @return
     * @author shengen.T
     * @createDate 2017/9/29
     */
    @AuthAccessRequired
    @ResponseBody
    @RequestMapping(path = "/getPushInfoPage" , method = RequestMethod.POST)
    public Object getPushInfoPage(OperatePushInfoPageDTO pageDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperatePushInfoPageDTO.class,request);
            if(null == pageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            pageDTO.setInfoType(PushInfoTypeEnum.OPERATE_PUSH_INFO_TYPE.getType());
            Page<OperatePushInfoVO> voList= iOperatePushInfoServiceClient.getPushInfoPage(pageDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),voList);
        }catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送getPage】获取分页信息失败",e);
        }
        return messageVO;
    }

    /*
    @Autowired
    IIotTpJPushClientServiceClient iIotTpJPushClientServiceClient;
    @RequestMapping(path = "/jpush/test" , method = RequestMethod.POST)
    public  void test(@RequestParam("msg") String msg,@RequestParam("projectId") String projectId,@RequestParam("userPhone") String userPhone,@RequestParam("devSip") String devSip,@RequestParam("roomSip") String roomSip){
        try {
            boolean isok =iIotTpJPushClientServiceClient.pushWithTag(msg,projectId,userPhone,devSip,roomSip);
            if(isok){
                System.out.print("OK");
            }
        }catch (Exception e){
            throw new DataAccessException("推送失败",e);
        }

    }*/

    /**
     * <desc>
     *      获取app历史版本号列表
     * </desc>
     *
     * @author wangzhi
     * @createDate 2020/04/29
     */
    @ResponseBody
    @RequestMapping(path = "/getAppVersionList", method = RequestMethod.POST)
    public Object getAppVersionList() {
        try {
            List<String> appVersionList = iOperatePushInfoServiceClient.getAppVersionList();
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), appVersionList);
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送getAppVersionList】获取app历史版本号列表失败", e);
        }
    }

    /**
     * <desc>
     *      处理旧推送数据，一次性接口，将原来的app推送数据复制一份作为小程序的推送数据，发布后第一时间运行该接口
     * </desc>
     *
     * @author wangzhi
     * @createDate 2020/04/29
     */
    @UnParamEncrypt
    @UnAuthAccessRequired
    @ResponseBody
    @LoggerInfo(operateDesc = LogDescConstant.COPY_OLD_PUSHINFO_LOG_DESC)
    @RequestMapping(path = "/copyAppPushInfoToMiniProgram")
    public Object copyAppPushInfoToMiniProgram() {
        try {
            Integer rows = iOperatePushInfoServiceClient.copyAppPushInfoToMiniProgram();
            if (rows >= 0) {
                return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) ("执行成功，已复制push_info中APP至小程序数据" + rows + "条"));
            } else {
                return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) "已执行过该接口，请勿重复执行！");
            }
        } catch (Exception e) {
            throw new DataAccessException("【运营_运营管理_消息推送copyAppPushInfoToMiniProgram】处理旧推送数据失败", e);
        }
    }

}
